package artid.com.arttogo.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.activity.OrganizationActivity;
import artid.com.arttogo.bean.MyComment;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.task.CallbackSky;
import artid.com.arttogo.utils.VeDate;

public class PersonCommentAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	CallbackSky cs;
	public List<MyComment> listdata;
	private ImageLoader mImageLoader;

	// String iconUrl;
	public PersonCommentAdapter(Context mContext) {
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
		listdata = new ArrayList<MyComment>();
		RequestQueue mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}

	public void setdata(List<MyComment> listdata) {
		this.listdata = listdata;
		// iconUrl = iconurl;
		notifyDataSetChanged();
	}

	public void addlist(List<MyComment> listdata) {
		this.listdata.addAll(listdata);
		// iconUrl = iconurl;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listdata.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_fragment_comment,
					null);
			vh.iv = (ImageView) convertView
					.findViewById(R.id.item_attention_photo);
			vh.textname = (TextView) convertView
					.findViewById(R.id.item_attention_name);
			vh.texttime = (TextView) convertView
					.findViewById(R.id.item_attention_message);
			vh.textmsg = (TextView) convertView
					.findViewById(R.id.item_attention_mainmsg);
			vh.isauth = (ImageView) convertView
					.findViewById(R.id.item_attention_auth);
			vh.artname = (TextView) convertView
					.findViewById(R.id.item_comment_artname);
			vh.mainImg = (ImageView) convertView
					.findViewById(R.id.item_attention_mainimg);
			vh.cmsg1 = (TextView) convertView.findViewById(R.id.item_comment1);
			vh.cmsg2 = (TextView) convertView.findViewById(R.id.item_comment2);
			vh.line = (View) convertView.findViewById(R.id.view_line_comment);

			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		vh.isauth.setVisibility(View.GONE);
		final MyComment item = listdata.get(position);

		vh.artname.setText(item.getTitle());
		vh.textname.setText(item.getRealname());
		vh.texttime.setText(VeDate.getStrMDTime(item.getCreate_time()));
	
		if (item.getDetail().equals("暂无")|| TextUtils.isEmpty(item.getDetail())) {
			vh.textmsg.setText("描述信息暂无");
		}else {
			vh.textmsg.setText(item.getDetail());
		}
		vh.cmsg1.setText(item.getNo1());
		if (!TextUtils.isEmpty(item.getNo2())) {
			vh.cmsg2.setText(item.getNo2());
		} else {
			vh.line.setVisibility(View.GONE);
			vh.cmsg2.setVisibility(View.GONE);
		}
		if (item.getIs_auth().equals("2")) {
			vh.isauth.setVisibility(View.VISIBLE);
		}else {
			vh.isauth.setVisibility(View.GONE);
		}
		if (item.getHeadurl().contains("http")) {
			ImageListener listener = ImageLoader.getImageListener(vh.iv,
					R.drawable.ic_menu_rotate, R.drawable.ic_menu_rotate);

			mImageLoader.get(item.getHeadurl(), listener);
		}

		if (item.getPic_url().contains("http")) {

			Glide.with(mContext).load(item.getPic_url() + AppUtil.picWidthUrl)
					.placeholder(R.drawable.ic_menu_rotate)
					.error(R.drawable.ic_menu_rotate).into(vh.mainImg);
			// ImageListener listener2 = ImageLoader
			// .getImageListener(vh.mainImg,
			// R.drawable.ic_menu_rotate,
			// R.drawable.ic_menu_rotate);
			// mImageLoader.get(item.getPic_url()+AppUtil.picWidthUrl,
			// listener2);
		} 
			vh.iv.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (TextUtils.isEmpty(item.getArtist_id())) {
					return;
				}
				if (item.getIs_type().equals("1")) {
					Intent intent = new Intent(mContext,CollecterActivity.class);
					intent.putExtra("id", item.getArtist_id());
					mContext.startActivity(intent);
				}else if (item.getIs_type().equals("2")) {
					Intent intent = new Intent(mContext,OrganizationActivity.class);
					intent.putExtra("id", item.getArtist_id());
					mContext.startActivity(intent);
				}
			
			}
		});

		return convertView;
	}

	// View.OnClickListener listenerlike(final String id){
	// return new View.OnClickListener() {
	//
	// @Override
	// public void onClick(View arg0) {
	// // TODO Auto-generated method stub
	// if (!AppUtil.isNeedLogin()) {
	// reqisLike(id);
	// }
	// }
	// };
	// }
	class ViewHolder {
		TextView textname, textmsg, texttime, cmsg2, cmsg1, artname;
		ImageView iv, mainImg, isauth, iv_comment;
		View line;

	}

}
